import { reactive, toRefs } from 'vue';
import { getDicts } from '@/api/system/dict-data';
import { listType } from '@/api/system/dict';
import { Message } from '@arco-design/web-vue';
// 引入封装数字字典接口
export const getDict = (...args: any[]) => {
  const res: any = reactive({});
  return (() => {
    args.forEach((d) => {
      res[d] = [];
      getDicts(d).then(({ data }: { data: any }) => {
        res[d] = data.map(
          (p: { [x: string]: any; dictLabel: any; dictValue: any }) => ({
            ...p,
            'label': p.dictLabel,
            'value': p.dictValue,
            'default-checked': p.default,
          })
        );
      });
    });
    return toRefs(res);
  })();
};

export const selectDictLabel = (list: any, data: any) => {
  return list.find(
    (item: { value: any }) => Number(item.value) === Number(data)
  ).label;
};
export const getDictTypes = async (
  params: any,
  keyName: { value: string | number; label: string | number }
) => {
  const { code, rows, msg } = await listType(params);
  if (code !== 200) {
    Message.error(msg);
    return [];
  }
  return rows.map((item: { [x: string]: any }) => ({
    value: item[keyName.value],
    label: item[keyName.label],
  }));
};
export default getDict;
// {
//   key: 1,
//   c: [ v1List,v2List,v3List ]
// }
